
-- The hero is the character played by the player.
-- He can jump, throw knifes at the enemies and guards and briefly run.
-- If shot by guard or enemies, he dies and has to restart the stage.

hero = {
  initialX = 100,
  x = 100,
  y = love.graphics.getHeight() -95 - (96/2),
  -- Sprite and Frame Variables
  spriteX = 70,
  spriteY = 97,
  frame = 0,
  frametime = 0,
  charQuads = {},
  charImage = love.graphics.newImage("images/sprites/hero.png"),
  -- Moving Variables
  speed = 30,
  direction = 2, -- Frontal Direction
  -- Jump Variables
  jumpTime = 0,
  jump = false,
  -- Boost Variables
  boost = false,
  boostTime = 0,
  -- Knife Variables
  knifes = {},
  -- Game Variables
  --life = 100,
  hidden = false,
  found = false,
  charQuadsHidden = {},
  charImageHidden = love.graphics.newImage("images/sprites/heroHidden.png"),

  loadImage = function()
    local count = 1  
    for j = 0, 3, 1 do
      for i = 0, 2, 1 do
        if(hero.charQuads[j] == nil) then 
          hero.charQuads[j] = {}
        end
        hero.charQuads[j][i] = love.graphics.newQuad(i * hero.spriteX, j * hero.spriteY, hero.spriteX, hero.spriteY, hero.charImage:getWidth(), hero.charImage:getHeight())
        count = count + 1
      end
    end
  end,
  
  loadImageHidden = function()
    local count = 1  
    for j = 0, 3, 1 do
      for i = 0, 2, 1 do
        if(hero.charQuadsHidden[j] == nil) then 
          hero.charQuadsHidden[j] = {}
        end
        hero.charQuadsHidden[j][i] = love.graphics.newQuad(i * hero.spriteX, j * hero.spriteY, hero.spriteX, hero.spriteY, hero.charImageHidden:getWidth(), hero.charImageHidden:getHeight())
        count = count + 1
      end
    end
  end,

  animate = function(dt)
    hero.frametime = hero.frametime + dt
    if hero.frametime > 0.1 then      
      hero.frame = hero.frame + 1       
      hero.frametime = 0
    end    
    if hero.charQuads[hero.direction][hero.frame] == nil then
      hero.frame = 0
    end
  end,

  move = function(dir, dt)  
    if dir == "down" then
      hero.direction = 2
      --So character does not pass the ground
      if(hero.y <= love.window.getHeight() - 95) then -- 115 is the floor size
        hero.y = hero.y + hero.speed * dt
      end
    elseif dir == "up" then
      hero.direction = 0
      --Removed so character does not fly
      --hero.y = hero.y - hero.speed * dt          
    elseif dir == "left" then
      hero.direction = 3
      hero.x = hero.x - hero.speed * dt
    elseif dir == "right" then
      hero.direction = 1
      hero.x = hero.x + hero.speed * dt
    end  
    hero.animate(dt) 
  end,

  updateGround = function(dt)
    if(hero.y <= love.window.getHeight() - 95) then
      hero.y = hero.y + (100 * dt)
    end
  end,

  checkJump = function(dt)
    if hero.jump then
      hero.y = hero.y - (300 * dt)
      hero.jumpTime = hero.jumpTime + dt
      if hero.jumpTime > 0.3 then
        hero.jumpTime = 0
        hero.jump = false
      end
    end
  end,

  checkVelocity = function(dt)
    if hero.boost then
      hero.speed = hero.speed + 20
      hero.boostTime = hero.boostTime + dt
      if hero.boostTime > 0.5 then
        hero.boostTime = 0
        hero.boost = false
        hero.speed = 100
      end
    end
  end,
  
}